package com.hmall.api.config;

import com.hmall.api.fallback.CartClientFallback;
import com.hmall.api.fallback.ItemClientFallback;
import com.hmall.api.fallback.TradeClientFallback;
import com.hmall.api.fallback.UserClientFallback;
import com.hmall.common.utils.UserContext;
import feign.Logger;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;

public class DefaultFeginConfig {
    @Bean
    public UserClientFallback userClientFallback(){
        return new UserClientFallback();
    }
    @Bean
    public TradeClientFallback tradeClientFallback(){
        return new TradeClientFallback();
    }

    @Bean
    public CartClientFallback cartClientFallback(){
        return new CartClientFallback();
    }

    //注册关于商品远程调用的客户端实例
    @Bean
    public ItemClientFallback itemClientFallback(){
        return new ItemClientFallback();
    }

    @Bean
    public Logger.Level feignLoggerLevel (){
        return Logger.Level.FULL;
    }

    //fegin请求拦截器
    @Bean
    public RequestInterceptor feignRequestInterceptor(){
        return new RequestInterceptor() {

            @Override
            public void apply(RequestTemplate template) {
                //获取线程变量中的用户id
                Long userId = UserContext.getUser();
                if (userId != null){
                    template.header("user-info",userId.toString());
                }
            }
        };

    }
}
